package com.iot.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.iot.core.JsonHelper;
import com.iot.core.LResult;
import com.iot.core.PageData;
import com.iot.pojo.MTusers;
import com.iot.service.TusersService;
import com.iot.util.Common;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/system/user/")
public class TUsersController extends BaseController {
    @Autowired
    private TusersService service;

    @ResponseBody
    @RequestMapping("save")
    public String save(@RequestParam String bean) {
        try {

            MTusers entity = JsonHelper.toBean(bean, MTusers.class);
            entity.setBADMIN(0);
            if (entity.getUSERS_ID() > 0) {
                service.update(entity);
            } else {
                service.insert(entity);

            }
            return LResult.Success(entity.getUSERS_ID()).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("get")
    public String getByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            MTusers bean = service.single(id);
            return LResult.Success(bean).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("del")
    public String deleteByid(@RequestParam Integer id) {
        try {
            //  String menu = request.getParameter("menu");
            service.deleteById(id);
            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("query")
    public String queryByPage() {

        try {
            //" AND SNAME LIKE '" + key + "%'"
            //   Map map = request.getParameterMap(); // 获取所有参数
            PageData map = getPageData();
            if (!map.containsKey("appId")) {
                map.put("appId", 0);
            }

            if (!map.containsKey("keyword")) {
                map.put("keyword", "");
            }
            if (!map.containsKey("sname")) {
                map.put("sname", "");
            }
            if (!map.containsKey("deptId")) {
                map.put("deptId", 0);
            }
            if (!map.containsKey("roleId")) {
                map.put("roleId", 0);
            }
            if (!map.containsKey("empId")) {
                map.put("empId", 0);
            }
            PageHelper.startPage(Common.toInt(map.get("page")), Common.toInt(map.get("limit")));
            List<Map<String, Object>> list = service.fetchByPage(map);
            PageInfo<Map<String, Object>> pageInfo = new PageInfo<>(list);
            return LResult.Success(pageInfo.getTotal(), pageInfo.getList()).toJson();
        } catch (Exception ex) {
            return  LResult.Error("数据错误").toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initList")
    public String initList(int appId) {
        try {
            Map<String, Object> map = service.initList(appId);
            return LResult.Success(map).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("initEdit")
    public String initEdit(int appId) {
        try {
            Map<String, Object> map = service.initEdit(appId);
            return LResult.Success(map).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

    @ResponseBody
    @RequestMapping("getUserById")
    public String getUserById() {
        try {
            PageData map = getPageData();
            if (!map.containsKey("userId")) {
                map.put("userId", 0);
            }
            int userId = Common.toInt(map.get("userId"));
            Map<String, Object> model = service.getUserById(userId);
            return LResult.Success(model).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }


    @ResponseBody
    @RequestMapping("getUserByEmpId")
    public String getUserByEmpId(int empId) {
        try {
            int count = service.getUserByEmpId(empId);
            return LResult.Success(count).toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }
    @ResponseBody
    @RequestMapping("changePwd")
    public String changePwd(int userId, String oldPwd, String newPwd)
    {
        try {
            service.changePwd(userId,oldPwd,newPwd);
            return LResult.Success().toJson();
        } catch (Exception ex) {
            return LResult.Error(ex.getMessage()).toJson();
        }
    }

}
